home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / DirectMusic / PlayAudio / frmAudio.frm (.txt) next >
Encoding:
Visual Basic Form  |  2001-10-08  |  13.5 KB  |  405 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
  3. Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
  4. Begin VB.Form frmAudio 
  5.    BorderStyle     =   1  'Fixed Single
  6.    Caption         =   "Play Audio"
  7.    ClientHeight    =   2520
  8.    ClientLeft      =   150
  9.    ClientTop       =   435
  10.    ClientWidth     =   4890
  11.    Icon            =   "frmAudio.frx":0000
  12.    LinkTopic       =   "Form1"
  13.    MaxButton       =   0   'False
  14.    ScaleHeight     =   2520
  15.    ScaleWidth      =   4890
  16.    StartUpPosition =   3  'Windows Default
  17.    Begin VB.Frame Frame1 
  18.       Caption         =   "Master Volume"
  19.       Height          =   675
  20.       Index           =   1
  21.       Left            =   2520
  22.       TabIndex        =   9
  23.       Top             =   1680
  24.       Width           =   2295
  25.       Begin MSComctlLib.Slider sldVolume 
  26.          Height          =   195
  27.          Left            =   180
  28.          TabIndex        =   10
  29.          Top             =   420
  30.          Width           =   1995
  31.          _ExtentX        =   3519
  32.          _ExtentY        =   344
  33.          _Version        =   393216
  34.          LargeChange     =   1000
  35.          SmallChange     =   100
  36.          Min             =   -2500
  37.          Max             =   200
  38.          SelStart        =   200
  39.          TickFrequency   =   500
  40.          Value           =   200
  41.       End
  42.       Begin VB.Label lbl 
  43.          BackStyle       =   0  'Transparent
  44.          Caption         =   "Max"
  45.          Height          =   255
  46.          Index           =   3
  47.          Left            =   1860
  48.          TabIndex        =   12
  49.          Top             =   180
  50.          Width           =   315
  51.       End
  52.       Begin VB.Label lbl 
  53.          BackStyle       =   0  'Transparent
  54.          Caption         =   "Min"
  55.          Height          =   255
  56.          Index           =   1
  57.          Left            =   180
  58.          TabIndex        =   11
  59.          Top             =   180
  60.          Width           =   315
  61.       End
  62.    End
  63.    Begin VB.Frame fraTempo 
  64.       Caption         =   "Tempo"
  65.       Height          =   675
  66.       Left            =   60
  67.       TabIndex        =   8
  68.       Top             =   1680
  69.       Width           =   2295
  70.       Begin MSComctlLib.Slider sldTempo 
  71.          Height          =   195
  72.          Left            =   120
  73.          TabIndex        =   13
  74.          Top             =   420
  75.          Width           =   1995
  76.          _ExtentX        =   3519
  77.          _ExtentY        =   344
  78.          _Version        =   393216
  79.          Max             =   30
  80.          SelStart        =   10
  81.          TickFrequency   =   5
  82.          Value           =   10
  83.       End
  84.       Begin VB.Label lbl 
  85.          BackStyle       =   0  'Transparent
  86.          Caption         =   "Fast"
  87.          Height          =   255
  88.          Index           =   6
  89.          Left            =   1680
  90.          TabIndex        =   16
  91.          Top             =   180
  92.          Width           =   375
  93.       End
  94.       Begin VB.Label lbl 
  95.          BackStyle       =   0  'Transparent
  96.          Caption         =   "Normal"
  97.          Height          =   255
  98.          Index           =   5
  99.          Left            =   540
  100.          TabIndex        =   15
  101.          Top             =   180
  102.          Width           =   615
  103.       End
  104.       Begin VB.Label lbl 
  105.          BackStyle       =   0  'Transparent
  106.          Caption         =   "Slow"
  107.          Height          =   255
  108.          Index           =   4
  109.          Left            =   120
  110.          TabIndex        =   14
  111.          Top             =   180
  112.          Width           =   375
  113.       End
  114.    End
  115.    Begin VB.CommandButton cmdExit 
  116.       Caption         =   "E&xit"
  117.       Height          =   315
  118.       Left            =   3840
  119.       TabIndex        =   7
  120.       Top             =   1260
  121.       Width           =   975
  122.    End
  123.    Begin VB.CheckBox chkLoop 
  124.       Caption         =   "Loop Audio"
  125.       Height          =   255
  126.       Left            =   60
  127.       TabIndex        =   6
  128.       Top             =   1320
  129.       Width           =   1155
  130.    End
  131.    Begin VB.TextBox txtFile 
  132.       BackColor       =   &H8000000F&
  133.       Height          =   285
  134.       Left            =   1140
  135.       Locked          =   -1  'True
  136.       TabIndex        =   5
  137.       Top             =   900
  138.       Width           =   3675
  139.    End
  140.    Begin VB.CommandButton cmdOpen 
  141.       Caption         =   "&Audio File"
  142.       Height          =   315
  143.       Left            =   120
  144.       TabIndex        =   0
  145.       Top             =   900
  146.       Width           =   975
  147.    End
  148.    Begin VB.CommandButton cmdPlay 
  149.       Caption         =   "&Play"
  150.       Enabled         =   0   'False
  151.       Height          =   315
  152.       Left            =   1320
  153.       TabIndex        =   1
  154.       Top             =   1260
  155.       Width           =   975
  156.    End
  157.    Begin VB.CommandButton cmdStop 
  158.       Caption         =   "&Stop"
  159.       Enabled         =   0   'False
  160.       Height          =   315
  161.       Left            =   2340
  162.       TabIndex        =   2
  163.       Top             =   1260
  164.       Width           =   975
  165.    End
  166.    Begin MSComDlg.CommonDialog cdlOpen 
  167.       Left            =   3000
  168.       Top             =   0
  169.       _ExtentX        =   847
  170.       _ExtentY        =   847
  171.       _Version        =   393216
  172.       Flags           =   4
  173.    End
  174.    Begin VB.Image Image1 
  175.       Height          =   480
  176.       Left            =   60
  177.       Picture         =   "frmAudio.frx":0442
  178.       Top             =   60
  179.       Width           =   480
  180.    End
  181.    Begin VB.Label lbl 
  182.       BackStyle       =   0  'Transparent
  183.       Caption         =   "Copyright (C) 1999-2001 Microsoft Corporation All Rights Reserved."
  184.       Height          =   495
  185.       Index           =   2
  186.       Left            =   600
  187.       TabIndex        =   4
  188.       Top             =   300
  189.       Width           =   3015
  190.    End
  191.    Begin VB.Label lbl 
  192.       BackStyle       =   0  'Transparent
  193.       Caption         =   "Play Audio Sample"
  194.       Height          =   255
  195.       Index           =   0
  196.       Left            =   600
  197.       TabIndex        =   3
  198.       Top             =   60
  199.       Width           =   2655
  200.    End
  201. Attribute VB_Name = "frmAudio"
  202. Attribute VB_GlobalNameSpace = False
  203. Attribute VB_Creatable = False
  204. Attribute VB_PredeclaredId = True
  205. Attribute VB_Exposed = False
  206. Option Explicit
  207. Option Compare Text
  208. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  209. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  210. '  File:       frmAudio.frm
  211. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  212. Implements DirectXEvent8
  213. Private dx As New DirectX8
  214. 'We need a loader object and a performance object
  215. 'We will play everything on our default audio path, so we do not need an audiopath object
  216. Private dmp As DirectMusicPerformance8
  217. Private dml As DirectMusicLoader8
  218. Private dmSeg As DirectMusicSegment8
  219. 'Our event handle
  220. Private dmEvent As Long
  221. 'API declare for windows folder
  222. Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
  223. Private Sub cmdExit_Click()
  224.     Unload Me 'Cleanup happens in form unload
  225. End Sub
  226. Private Sub cmdOpen_Click()
  227.     Static sCurDir As String
  228.     Static lFilter As Long
  229.     'We want to open a file now
  230.     cdlOpen.flags = cdlOFNHideReadOnly Or cdlOFNFileMustExist
  231.     cdlOpen.FilterIndex = lFilter
  232.     cdlOpen.Filter = "Wave Files (*.wav)|*.wav|Music Files (*.mid;*.rmi)|*.mid;*.rmi|Segment Files (*.sgt)|*.sgt|All Audio Files|*.wav;*.mid;*.rmi;*.sgt|All Files (*.*)|*.*"
  233.     cdlOpen.FileName = vbNullString
  234.     If sCurDir = vbNullString Then
  235.         'Set the init folder to \windows\media if it exists.  If not, set it to the \windows folder
  236.         Dim sWindir As String
  237.         sWindir = Space$(255)
  238.         If GetWindowsDirectory(sWindir, 255) = 0 Then
  239.             'We couldn't get the windows folder for some reason, use the c:\
  240.             cdlOpen.InitDir = "C:\"
  241.         Else
  242.             Dim sMedia As String
  243.             sWindir = Left$(sWindir, InStr(sWindir, Chr$(0)) - 1)
  244.             If Right$(sWindir, 1) = "\" Then
  245.                 sMedia = sWindir & "Media"
  246.             Else
  247.                 sMedia = sWindir & "\Media"
  248.             End If
  249.             If Dir$(sMedia, vbDirectory) <> vbNullString Then
  250.                 cdlOpen.InitDir = sMedia
  251.             Else
  252.                 cdlOpen.InitDir = sWindir
  253.             End If
  254.         End If
  255.     Else
  256.         cdlOpen.InitDir = sCurDir
  257.     End If
  258.     On Local Error GoTo ClickedCancel
  259.     cdlOpen.CancelError = True
  260.     cdlOpen.ShowOpen   ' Display the Open dialog box
  261.     'Save the current information
  262.     sCurDir = GetFolder(cdlOpen.FileName)
  263.     'Set the search folder to this one so we can auto download anything we need
  264.     dml.SetSearchDirectory sCurDir
  265.     lFilter = cdlOpen.FilterIndex
  266.             
  267.     On Local Error GoTo NoLoadSegment
  268.     'Before we load the segment stop one if it's playing
  269.     cmdStop_Click
  270.     'Now let's load the segment
  271.     If FileLen(cdlOpen.FileName) = 0 Then Err.Raise 5
  272.     EnableTempoControl (Right$(cdlOpen.FileName, 4) <> ".wav")
  273.     Set dmSeg = dml.LoadSegment(cdlOpen.FileName)
  274.     If (Right$(cdlOpen.FileName, 4) = ".mid") Or (Right$(cdlOpen.FileName, 4) = ".rmi") Or (Right$(cdlOpen.FileName, 5) = ".midi") Then
  275.         dmSeg.SetStandardMidiFile
  276.     End If
  277.     txtFile.Text = cdlOpen.FileName
  278.     EnablePlayUI True
  279.     sldTempo.Value = 10
  280.     sldTempo_Click
  281.     Exit Sub
  282. NoLoadSegment:
  283.     MsgBox "Couldn't load this segment", vbOKOnly Or vbCritical, "Couldn't load"
  284. ClickedCancel:
  285. End Sub
  286. Private Sub cmdPlay_Click()
  287.     If Not (dmSeg Is Nothing) Then
  288.         If chkLoop.Value = vbChecked Then
  289.             dmSeg.SetRepeats -1 'Loop infinitely
  290.         Else
  291.             dmSeg.SetRepeats 0 'Don't loop
  292.         End If
  293.         dmp.PlaySegmentEx dmSeg, DMUS_SEGF_DEFAULT, 0
  294.         EnablePlayUI False
  295.     End If
  296. End Sub
  297. Private Sub cmdStop_Click()
  298.     If Not (dmSeg Is Nothing) Then dmp.StopEx dmSeg, 0, 0
  299.     EnablePlayUI True
  300. End Sub
  301. Private Sub DirectXEvent8_DXCallback(ByVal eventid As Long)
  302.     Dim dmNotification As DMUS_NOTIFICATION_PMSG
  303.     'We only have one event
  304.     If Not dmp.GetNotificationPMSG(dmNotification) Then
  305.         MsgBox "Error processing this Notification", vbOKOnly Or vbInformation, "Cannot Process."
  306.         Exit Sub
  307.     Else
  308.         If dmNotification.lNotificationOption = DMUS_NOTIFICATION_SEGEND Then 'The segment has ended
  309.             EnablePlayUI True
  310.         End If
  311.     End If
  312. End Sub
  313. Private Sub Form_Load()
  314.     InitAudio
  315.     EnableTempoControl False
  316. End Sub
  317. Private Sub InitAudio()
  318.     On Error GoTo FailedInit
  319.     'We need to create our objects now
  320.     Set dmp = dx.DirectMusicPerformanceCreate
  321.     Set dml = dx.DirectMusicLoaderCreate
  322.     Dim dmusAudio As DMUS_AUDIOPARAMS
  323.     'Now call init audio
  324.     dmp.InitAudio Me.hWnd, DMUS_AUDIOF_ALL, dmusAudio, Nothing, DMUS_APATH_SHARED_STEREOPLUSREVERB, 128
  325.     dmp.SetMasterAutoDownload True
  326.     'Now add a notification for the segment
  327.     dmp.AddNotificationType DMUS_NOTIFY_ON_SEGMENT
  328.     'Create an event so we can receive these
  329.     dmEvent = dx.CreateEvent(Me)
  330.     dmp.SetNotificationHandle dmEvent
  331.     Exit Sub
  332. FailedInit:
  333.     MsgBox "Could not initialize DirectMusic." & vbCrLf & "This sample will exit.", vbOKOnly Or vbInformation, "Exiting..."
  334.     CleanupAudio
  335.     Unload Me
  336.     End
  337. End Sub
  338. Private Sub CleanupAudio()
  339.     'Cleanup everything
  340.     On Error Resume Next
  341.     dmp.RemoveNotificationType DMUS_NOTIFY_ON_SEGMENT
  342.     dx.DestroyEvent dmEvent
  343.     If Not (dmSeg Is Nothing) Then dmp.StopEx dmSeg, 0, 0
  344.     Set dmSeg = Nothing
  345.     Set dml = Nothing
  346.     If Not (dmp Is Nothing) Then dmp.CloseDown
  347.     Set dmp = Nothing
  348. End Sub
  349. Private Sub Form_Unload(Cancel As Integer)
  350.     CleanupAudio
  351. End Sub
  352. Private Function GetFolder(ByVal sFile As String) As String
  353.     Dim lCount As Long
  354.     For lCount = Len(sFile) To 1 Step -1
  355.         If Mid$(sFile, lCount, 1) = "\" Then
  356.             GetFolder = Left$(sFile, lCount)
  357.             Exit Function
  358.         End If
  359.     Next
  360.     GetFolder = vbNullString
  361. End Function
  362. Public Sub EnablePlayUI(fEnable As Boolean)
  363.     'Enable/Disable the buttons
  364.     If fEnable Then
  365.         chkLoop.Enabled = True
  366.         cmdStop.Enabled = False
  367.         cmdPlay.Enabled = True
  368.         cmdOpen.Enabled = True
  369.         cmdPlay.SetFocus
  370.     Else
  371.         chkLoop.Enabled = False
  372.         cmdStop.Enabled = True
  373.         cmdPlay.Enabled = False
  374.         cmdOpen.Enabled = False
  375.         cmdStop.SetFocus
  376.     End If
  377. End Sub
  378. Private Sub sldTempo_Click()
  379.     'Update the tempo now
  380.     dmp.SetMasterTempo (sldTempo.Value / 10)
  381. End Sub
  382. Private Sub sldTempo_Scroll()
  383.     sldTempo_Click
  384. End Sub
  385. Private Sub sldVolume_Click()
  386.     sldVolume_Scroll
  387. End Sub
  388. Private Sub sldVolume_Scroll()
  389.     'Update the volume
  390.     dmp.SetMasterVolume sldVolume.Value
  391. End Sub
  392. Private Sub EnableTempoControl(ByVal fEnable As Boolean)
  393.     'If this is a wave file, turn off tempo control
  394.     fraTempo.Enabled = fEnable
  395.     sldTempo.Enabled = fEnable
  396.     lbl(4).Enabled = fEnable
  397.     lbl(5).Enabled = fEnable
  398.     lbl(6).Enabled = fEnable
  399.     If Not fEnable Then
  400.         sldTempo.TickStyle = sldNoTicks
  401.     Else
  402.         sldTempo.TickStyle = sldBottomRight
  403.     End If
  404. End Sub
  405.